package com.you.atlas.web.place.module.action;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.service.uribroker.uri.URIBroker;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.you.atlas.biz.place.service.CommentService;
import com.you.atlas.web.common.util.HtmlEscapeUtil;
import com.you.common.webx.AuthContext;
import com.you.place.dal.dao.dataobject.CommentDO;

public class CommentAction {

    @Autowired
    private CommentService commentService;

    public void doPostByPhoto(@Param("comment") String comment, @Param("id") Long albumId,
                              @Param("pageNo") String pageNo, Navigator navigator) {
        if (postComment("photo", comment, albumId)) {
            URIBroker photoView = navigator.redirectTo("placeModule").withTarget("photoView.vm").uri();
            photoView.addQueryData("id", albumId).addQueryData("pageNo", pageNo).setReference("last");
        }
    }

    public void doPostByNote(@Param("comment") String comment, @Param("id") Long noteId,
                             @Param("pageNo") String pageNo, Navigator navigator) {
        if (postComment("note", comment, noteId)) {
            URIBroker noteView = navigator.redirectTo("placeModule").withTarget("noteView.vm").uri();
            noteView.addQueryData("id", noteId).addQueryData("pageNo", pageNo).setReference("last");
        }

    }

    private boolean postComment(String type, String comment, Long albumId) {
        Long memberId = AuthContext.getContext().getMemberId();
        if (memberId == -1) return false;
        CommentDO commentDO = new CommentDO();
        commentDO.setContent(comment);
        commentDO.setFkId(albumId);
        commentDO.setPostMemberId(memberId);
        commentDO.setType(type);
        return commentService.postComment(commentDO);
    }
}
